package com.ruoyi.drone.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.drone.domain.DroneTask;
import com.ruoyi.drone.domain.dto.PilotTaskDetailDTO;
import com.ruoyi.drone.service.IDroneTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.drone.mapper.DroneTaskExecutionMapper;
import com.ruoyi.drone.domain.DroneTaskExecution;
import com.ruoyi.drone.service.IDroneTaskExecutionService;

/**
 * 任务执行Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class DroneTaskExecutionServiceImpl implements IDroneTaskExecutionService 
{
    @Autowired
    private DroneTaskExecutionMapper droneTaskExecutionMapper;

    @Autowired
    private IDroneTaskService droneTaskService;

    /**
     * 查询任务执行
     * 
     * @param executionId 任务执行主键
     * @return 任务执行
     */
    @Override
    public DroneTaskExecution selectDroneTaskExecutionByExecutionId(Long executionId)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionByExecutionId(executionId);
    }

    /**
     * 查询任务执行列表
     * 
     * @param droneTaskExecution 任务执行
     * @return 任务执行
     */
    @Override
    public List<DroneTaskExecution> selectDroneTaskExecutionList(DroneTaskExecution droneTaskExecution)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionList(droneTaskExecution);
    }

    /**
     * 根据任务ID查询任务执行列表
     * 
     * @param taskId 任务ID
     * @return 任务执行集合
     */
    @Override
    public List<DroneTaskExecution> selectDroneTaskExecutionListByTaskId(Long taskId)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionListByTaskId(taskId);
    }

    /**
     * 根据飞手ID查询任务执行列表
     * 
     * @param pilotId 飞手ID
     * @return 任务执行集合
     */
    @Override
    public List<DroneTaskExecution> selectDroneTaskExecutionListByPilotId(Long pilotId)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionListByPilotId(pilotId);
    }

    /**
     * 根据申请ID查询任务执行
     * 
     * @param applicationId 申请ID
     * @return 任务执行
     */
    @Override
    public DroneTaskExecution selectDroneTaskExecutionByApplicationId(Long applicationId)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionByApplicationId(applicationId);
    }

    /**
     * 根据任务ID和飞手ID查询任务执行
     * 
     * @param taskId 任务ID
     * @param pilotId 飞手ID
     * @return 任务执行
     */
    @Override
    public DroneTaskExecution selectDroneTaskExecutionByTaskIdAndPilotId(Long taskId, Long pilotId)
    {
        return droneTaskExecutionMapper.selectDroneTaskExecutionByTaskIdAndPilotId(taskId, pilotId);
    }

    /**
     * 新增任务执行
     * 
     * @param droneTaskExecution 任务执行
     * @return 结果
     */
    @Override
    public int insertDroneTaskExecution(DroneTaskExecution droneTaskExecution)
    {
        droneTaskExecution.setCreateTime(DateUtils.getNowDate());
        droneTaskExecution.setStatus("0"); // 默认状态为进行中
        droneTaskExecution.setCompletionRate(BigDecimal.ZERO); // 默认完成度为0
        droneTaskExecution.setWorkHours(BigDecimal.ZERO); // 默认工作时长为0
        return droneTaskExecutionMapper.insertDroneTaskExecution(droneTaskExecution);
    }

    /**
     * 修改任务执行
     * 
     * @param droneTaskExecution 任务执行
     * @return 结果
     */
    @Override
    public int updateDroneTaskExecution(DroneTaskExecution droneTaskExecution)
    {
        droneTaskExecution.setUpdateTime(DateUtils.getNowDate());
        return droneTaskExecutionMapper.updateDroneTaskExecution(droneTaskExecution);
    }

    /**
     * 批量删除任务执行
     * 
     * @param executionIds 需要删除的任务执行主键
     * @return 结果
     */
    @Override
    public int deleteDroneTaskExecutionByExecutionIds(Long[] executionIds)
    {
        return droneTaskExecutionMapper.deleteDroneTaskExecutionByExecutionIds(executionIds);
    }

    /**
     * 删除任务执行信息
     * 
     * @param executionId 任务执行主键
     * @return 结果
     */
    @Override
    public int deleteDroneTaskExecutionByExecutionId(Long executionId)
    {
        return droneTaskExecutionMapper.deleteDroneTaskExecutionByExecutionId(executionId);
    }

    /**
     * 检查任务和飞手的执行记录是否唯一
     * 
     * @param taskId 任务ID
     * @param pilotId 飞手ID
     * @return 结果
     */
    @Override
    public boolean checkTaskExecutionUnique(Long taskId, Long pilotId)
    {
        int count = droneTaskExecutionMapper.checkTaskExecutionUnique(taskId, pilotId);
        return count == 0;
    }

    /**
     * 更新任务执行状态
     * 
     * @param droneTaskExecution 任务执行
     * @return 结果
     */
    @Override
    public int updateDroneTaskExecutionStatus(DroneTaskExecution droneTaskExecution)
    {
        droneTaskExecution.setUpdateTime(DateUtils.getNowDate());
        return droneTaskExecutionMapper.updateDroneTaskExecutionStatus(droneTaskExecution);
    }

    /**
     * 更新任务执行进度
     * 
     * @param droneTaskExecution 任务执行
     * @return 结果
     */
    @Override
    public int updateDroneTaskExecutionProgress(DroneTaskExecution droneTaskExecution)
    {
        droneTaskExecution.setUpdateTime(DateUtils.getNowDate());
        return droneTaskExecutionMapper.updateDroneTaskExecutionProgress(droneTaskExecution);
    }

    /**
     * 开始任务执行
     * 
     * @param droneTaskExecution 任务执行
     * @return 结果
     */
    @Override
    public int startTaskExecution(DroneTaskExecution droneTaskExecution)
    {
        // 检查是否已经存在执行记录
        if (!checkTaskExecutionUnique(droneTaskExecution.getTaskId(), droneTaskExecution.getPilotId())) {
            throw new RuntimeException("该任务已经存在执行记录，不能重复开始");
        }
        
        droneTaskExecution.setCreateBy(SecurityUtils.getUsername());
        droneTaskExecution.setCreateTime(DateUtils.getNowDate());
        droneTaskExecution.setStartTime(DateUtils.getNowDate());
        droneTaskExecution.setStatus("0"); // 进行中状态
        droneTaskExecution.setCompletionRate(BigDecimal.ZERO); // 初始完成度为0
        droneTaskExecution.setWorkHours(BigDecimal.ZERO); // 初始工作时长为0
        
        return droneTaskExecutionMapper.insertDroneTaskExecution(droneTaskExecution);
    }

    /**
     * 完成任务执行
     * 
     * @param executionId 执行ID
     * @param finalPrice 最终价格
     * @param qualityScore 质量评分
     * @return 结果
     */
    @Override
    public int completeTaskExecution(Long executionId, BigDecimal finalPrice, Integer qualityScore)
    {
        DroneTaskExecution execution = new DroneTaskExecution();
        execution.setExecutionId(executionId);
        execution.setStatus("1"); // 已完成
        execution.setEndTime(DateUtils.getNowDate());
        execution.setFinalPrice(finalPrice);
        execution.setQualityScore(qualityScore);
        execution.setCompletionRate(new BigDecimal("100.00")); // 完成度设为100%
        execution.setUpdateBy(SecurityUtils.getUsername());
        execution.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskExecutionMapper.completeDroneTaskExecution(execution);
    }

    /**
     * 暂停任务执行
     * 
     * @param executionId 执行ID
     * @return 结果
     */
    @Override
    public int pauseTaskExecution(Long executionId)
    {
        DroneTaskExecution execution = new DroneTaskExecution();
        execution.setExecutionId(executionId);
        execution.setStatus("2"); // 已暂停
        execution.setUpdateBy(SecurityUtils.getUsername());
        execution.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskExecutionMapper.pauseDroneTaskExecution(execution);
    }

    /**
     * 恢复任务执行
     * 
     * @param executionId 执行ID
     * @return 结果
     */
    @Override
    public int resumeTaskExecution(Long executionId)
    {
        DroneTaskExecution execution = new DroneTaskExecution();
        execution.setExecutionId(executionId);
        execution.setStatus("0"); // 进行中
        execution.setUpdateBy(SecurityUtils.getUsername());
        execution.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskExecutionMapper.updateDroneTaskExecutionStatus(execution);
    }

    /**
     * 取消任务执行
     * 
     * @param executionId 执行ID
     * @return 结果
     */
    @Override
    public int cancelTaskExecution(Long executionId)
    {
        DroneTaskExecution execution = new DroneTaskExecution();
        execution.setExecutionId(executionId);
        execution.setStatus("3"); // 已取消
        execution.setEndTime(DateUtils.getNowDate());
        execution.setUpdateBy(SecurityUtils.getUsername());
        execution.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskExecutionMapper.cancelDroneTaskExecution(execution);
    }

    /**
     * 更新工作进度
     * 
     * @param executionId 执行ID
     * @param completionRate 完成度
     * @param workContent 工作内容
     * @param workImages 工作图片
     * @param workVideos 工作视频
     * @param workHours 工作时长
     * @return 结果
     */
    @Override
    public int updateWorkProgress(Long executionId, BigDecimal completionRate, String workContent, String workImages, String workVideos, BigDecimal workHours)
    {
        DroneTaskExecution execution = new DroneTaskExecution();
        execution.setExecutionId(executionId);
        execution.setCompletionRate(completionRate);
        execution.setWorkContent(workContent);
        execution.setWorkImages(workImages);
        execution.setWorkVideos(workVideos);
        execution.setWorkHours(workHours);
        execution.setUpdateBy(SecurityUtils.getUsername());
        execution.setUpdateTime(DateUtils.getNowDate());
        
        return droneTaskExecutionMapper.updateDroneTaskExecutionProgress(execution);
    }

    /**
     * 查询飞手任务详情列表（包含任务信息）
     * 
     * @param pilotId 飞手ID
     * @return 任务详情集合
     */
    @Override
    public List<PilotTaskDetailDTO> selectPilotTaskDetailList(Long pilotId)
    {
        List<DroneTaskExecution> executionList = droneTaskExecutionMapper.selectDroneTaskExecutionListByPilotId(pilotId);
        List<PilotTaskDetailDTO> resultList = new java.util.ArrayList<>();
        
        for (DroneTaskExecution execution : executionList) {
            PilotTaskDetailDTO dto = new PilotTaskDetailDTO();
            dto.setExecution(execution);
            
            // 查询任务详情
            DroneTask task = droneTaskService.selectDroneTaskByTaskId(execution.getTaskId());
            dto.setTask(task);
            
            resultList.add(dto);
        }
        
        return resultList;
    }

    /**
     * 根据执行ID查询飞手任务详情
     * 
     * @param executionId 执行ID
     * @return 任务详情
     */
    @Override
    public PilotTaskDetailDTO selectPilotTaskDetailByExecutionId(Long executionId)
    {
        DroneTaskExecution execution = droneTaskExecutionMapper.selectDroneTaskExecutionByExecutionId(executionId);
        if (execution == null) {
            return null;
        }
        
        PilotTaskDetailDTO dto = new PilotTaskDetailDTO();
        dto.setExecution(execution);
        
        // 查询任务详情
        DroneTask task = droneTaskService.selectDroneTaskByTaskId(execution.getTaskId());
        dto.setTask(task);
        
        return dto;
    }
}
